package com.jhgsys.internal.base.controller;

import com.jhgsys.internal.base.dto.BaseCautionaryLessonDto;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.constant.ModuleManagerPermissionConstant;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.base.entity.BaseCautionaryLesson;
import com.jhgsys.internal.base.service.IBaseCautionaryLessonService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author zhangwei
 * @date 2024-09-05 09:48:17
 */
@Slf4j
@Validated
@Controller
public class BaseCautionaryLessonController extends BaseController {

    @Autowired
    private IBaseCautionaryLessonService baseCautionaryLessonService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseCautionaryLesson")
    @RequiresPermissions("baseCautionaryLesson:view")
    public String baseCautionaryLessonIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("base/baseCautionaryLesson/baseCautionaryLessonList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseCautionaryLesson/add")
    @RequiresPermissions("baseCautionaryLesson:add")
    public String baseCautionaryLessonAdd(){
        return FebsUtil.view("base/baseCautionaryLesson/baseCautionaryLessonEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseCautionaryLesson/update/{id}")
    @RequiresPermissions("baseCautionaryLesson:edit")
    public String baseCautionaryLessonUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("baseCautionaryLesson", baseCautionaryLessonService.findBaseCautionaryLessonById(id));
        return FebsUtil.view("base/baseCautionaryLesson/baseCautionaryLessonEdit");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     */
    @GetMapping("baseCautionaryLesson")
    @ResponseBody
    @RequiresPermissions("baseCautionaryLesson:view")
    public FebsResponse getAllBaseCautionaryLessons(BaseCautionaryLesson baseCautionaryLesson) {
        return new FebsResponse().success().data(baseCautionaryLessonService.findBaseCautionaryLessonList(baseCautionaryLesson));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     */
    @GetMapping("baseCautionaryLesson/list")
    @ResponseBody
    @RequiresPermissions("baseCautionaryLesson:view")
    public FebsResponse baseCautionaryLessonList(QueryRequest request, BaseCautionaryLesson baseCautionaryLesson) {
        //如果不是模块管理员，只能查看本部门的警示
        if(!hasPermission(ModuleManagerPermissionConstant.BASECAUTIONARY)){
            baseCautionaryLesson.setCautionaryDeptId(getCurrentUser().getDeptId());
        }
        Map<String, Object> dataTable = getDataTable(this.baseCautionaryLessonService.findBaseCautionaryLessonPage(request, baseCautionaryLesson));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 BaseCautionaryLesson
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     */
    @Log("新增BaseCautionaryLesson")
    @PostMapping("baseCautionaryLesson")
    @ResponseBody
    @RequiresPermissions("baseCautionaryLesson:add")
    public FebsResponse addBaseCautionaryLesson(@RequestBody BaseCautionaryLesson baseCautionaryLesson) {
        try {
            baseCautionaryLesson.setCreatedBy(getCurrentUser().getUserName());
            this.baseCautionaryLessonService.createBaseCautionaryLesson(baseCautionaryLesson);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增BaseCautionaryLesson失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 BaseCautionaryLesson
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     */
    @Log("逻辑删除BaseCautionaryLesson")
    @GetMapping("baseCautionaryLesson/delete/{id}")
    @ResponseBody
    @RequiresPermissions("baseCautionaryLesson:delete")
    public FebsResponse deleteBaseCautionaryLesson(@PathVariable("id") String baseCautionaryLessonIds) {
        try {
            BaseCautionaryLesson baseCautionaryLesson = new BaseCautionaryLesson();
            baseCautionaryLesson.setDeletedBy(getCurrentUser().getUserName());
            baseCautionaryLesson.setDeletedTime(new Date());
            baseCautionaryLesson.setDeletedFlg(FebsConstant.INVALID);
            this.baseCautionaryLessonService.deleteBaseCautionaryLesson(baseCautionaryLesson,baseCautionaryLessonIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseCautionaryLesson失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 BaseCautionaryLesson
    * @author zhangwei
    * @date 2024-09-05 09:48:17
    */
    @Log("物理删除BaseCautionaryLesson")
    @GetMapping("baseCautionaryLesson/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("baseCautionaryLesson:delete")
    public FebsResponse physicallyDeleteBaseCautionaryLesson(@PathVariable("id") String baseCautionaryLessonIds) {
        try {
            this.baseCautionaryLessonService.physicallyDeleteBaseCautionaryLesson(baseCautionaryLessonIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseCautionaryLesson失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 BaseCautionaryLesson
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     */
    @Log("修改BaseCautionaryLesson")
    @PostMapping("baseCautionaryLesson/update")
    @ResponseBody
    @RequiresPermissions("baseCautionaryLesson:edit")
    public FebsResponse updateBaseCautionaryLesson(@RequestBody BaseCautionaryLessonDto baseCautionaryLessonDto) {
        try {
            BaseCautionaryLesson baseCautionaryLesson = BeanHelper.copyProperties(baseCautionaryLessonDto, BaseCautionaryLesson.class);
            baseCautionaryLesson.setCautionaryFileInfo("");
            if(!ObjectUtils.isEmpty(baseCautionaryLessonDto.getUploadFileInfo())){
                baseCautionaryLesson.setCautionaryFileInfo(baseCautionaryLessonDto.getUploadFileInfo().toJSONString());
            }
            baseCautionaryLesson.setUpdatedBy(getCurrentUser().getUserName());
            baseCautionaryLesson.setStatus("1");
            baseCautionaryLesson.setStudyDate(new Date());
            this.baseCautionaryLessonService.updateBaseCautionaryLesson(baseCautionaryLesson);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改BaseCautionaryLesson失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

}
